package example;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 支绍昆 2018/10/12 0012
 */
public class FileUploadClientHandler extends ChannelInboundHandlerAdapter {

    private int byteRead;
    private volatile int start = 0 ;
    private volatile int lastLength = 0;
    private FileUpload fileUpload;
    public RandomAccessFile randomAccessFile;

    public FileUploadClientHandler(FileUpload fu){
        if (!fu.getFile().exists()){
            System.out.println("file not exists " + fu.getFile());
            return;
        }
        if (!fu.getFile().isFile()){
            System.out.println("Not a file " + fu.getFile());
            return;
        }
        this.fileUpload = fu;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        System.out.println("file upload end, channel inactive ");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx){
        System.out.println("channel active ");
        try {
            randomAccessFile = new RandomAccessFile(fileUpload.getFile(), "r");
            randomAccessFile.seek(fileUpload.getStarPos());

            lastLength = 1024 * 10;
            byte[] bytes = new byte[lastLength];
            if ((byteRead = randomAccessFile.read(bytes)) != -1) {
                fileUpload.setEndPos(byteRead);
                fileUpload.setBytes(bytes);
                ctx.writeAndFlush(fileUpload);
            }else {
                System.out.println("read file end.");
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof Integer){
            start = (Integer) msg;
            if (start != -1){
                randomAccessFile = new RandomAccessFile(fileUpload.getFile(), "r");
                randomAccessFile.seek(start);
                System.out.println("length:" + (randomAccessFile.length() - start));

                int a = (int) (randomAccessFile.length() - start);
                int b = (int) (randomAccessFile.length() / 1024*2);
                if (a < lastLength){
                    lastLength = a;
                }

                System.out.println("file length : " + randomAccessFile.length());
                System.out.println("start : " + start);
                System.out.println("a : " + a);
                System.out.println("b : " + b);

                byte[] bytes = new byte[lastLength];

                if ((byteRead = randomAccessFile.read(bytes)) != -1 && a > 0){
                    fileUpload.setEndPos(byteRead);
                    fileUpload.setBytes(bytes);
                    ctx.writeAndFlush(fileUpload);
                }
            }else {
                randomAccessFile.close();
                ctx.close();
                System.out.println("file read end ..." + byteRead);
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

}
